package dao;

import domin.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/*
* mybatis中针对,CRUD一共有四个注解
* @Select @Insert @Update @Delete
* */
@CacheNamespace(blocking = true)//开启二级缓存
public interface IUserDao {
    /*
     * 查询所有
     * */
    @Select("select * from user")
    @Results(id = "userMap",value = {
            @Result(id = true,column = "id",property = "userId"),
            @Result(column = "username",property = "userName"),
            @Result(column = "address",property = "userAddress"),
            @Result(column = "sex",property = "userSex"),
            @Result(column = "birthday",property = "userBirthday"),
            @Result(column = "id",property = "accounts",many = @Many(select = "dao.IAccountDao.findAccountByUid",fetchType = FetchType.LAZY))
    })
    List<User> findAll();

    /*
    * 根据ID查询用户
    * */
    @Select("select * from user where id=#{id}")
//    @ResultMap(value = {"userMap"})//resultMap的标准写法
    @ResultMap("userMap") //resultMap的简化写法,value的键名可以省略,如果大括号中只有一个元素,大括号也可以省略
    User findById(Integer userId);

    /*
    * 根据用户名模糊查询用户
    * */
    @Select("select * from user where username like #{username}")
    @ResultMap("userMap")
    List<User> findUserByname(String username);


}
